package com.example.crmweb.controller;


import com.example.crmweb.entity.Branch;
import com.example.crmweb.service.BranchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author -CRM-
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/branch")
public class BranchController {
    @Autowired
    private BranchService branchService;

    /**
     * 添加部门
     * @param branch
     * @return
     */
    @PostMapping("")
    public String createBranch(Branch branch){
        Integer res = branchService.createBranch(branch);
        if (res>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /**
     * 修改部门信息
     * @param id
     * @param branch
     * @return
     */
    @PutMapping("{id}")
    public String modifyBranch( @PathVariable("id") Integer id,Branch branch){
        Integer res = branchService.modifyBranch(branch);
        if (res>0){
            return "success";
        }else {
            return "fail";
        }
    }

    /**
     * 根据id查询 部门
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Optional<Branch> describeById(@PathVariable("id") Integer id){
        Optional<Branch> optional = branchService.describeById(id);
        return optional;
    }

    /**
     * 查询所有部门
     * @return
     */
    @GetMapping("list")
    public Optional<List<Branch>> describeList(){
        Optional<List<Branch>> branches = branchService.describeList();
        return branches;
    }

}
